package com.snail.snailvr;

import android.content.Context;
import android.content.Intent;
import android.support.multidex.MultiDex;

import com.activeandroid.ActiveAndroid;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;
import com.snail.snailvr.utils.ChannelUtil;
import com.snail.snailvr.views.MyUncaughtExceptionHandler;
import com.snailgame.fastdev.FastDevApplication;
import com.testin.agent.TestinAgent;
import com.testin.agent.TestinAgentConfig;
import com.umeng.analytics.AnalyticsConfig;

/**
 * Created by snail on 16/3/25.
 */
public class VRApp extends FastDevApplication {

    public static Context getInstance() {
        return getContext();
    }

    @Override
    public void onCreate() {
        super.onCreate();
//        if(BuildConfig.DEBUG)
//            LeakCanary.install(this);
        ActiveAndroid.initialize(this);
        startService(new Intent(this, SnailVrService.class));
        initStatistics();
//        MyUncaughtExceptionHandler.getInstance().init(this);
        initTestIn();

    }
    /**
     * 第三方统计配置
     */
    private void initStatistics() {
        // 友盟统计
        AnalyticsConfig.setChannel(ChannelUtil.getChannelID());
        AnalyticsConfig.setAppkey(this, "57970475e0f55acd8300116c");

    }
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        ActiveAndroid.dispose();
    }

    private Tracker mTracker;
    synchronized public Tracker getDefaultTracker() {
        if (mTracker == null) {
            GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
            // To enable debug logging use: adb shell setprop log.tag.GAv4 DEBUG
            mTracker = analytics.newTracker(R.xml.global_tracker);
        }
        return mTracker;
    }

    private void initTestIn() {
        TestinAgentConfig config =  new  TestinAgentConfig.Builder(this.getApplicationContext())
                .withAppKey("5886601e0f1544cb6021d16536840012")                // Appkey of your appliation, required
                .withAppChannel("")            // Channel of your application
                .withUserInfo("")              // User infomation like login account of user
                .withDebugModel( true )        // Output the crash log in local if you open debug mode
                .withErrorActivity( true )     // Output the activity info in crash or error log
                .withCollectNDKCrash( true )   // Collect NDK crash or not if you use our NDK
                .withOpenCrash( true )         // Monitor crash if true
                .withReportOnlyWifi( true )    // Report data only on wifi mode
                .withReportOnBack( true )      // allow to report data when application in background
                .build();
        TestinAgent.init(config);
        TestinAgent.setTestinUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
    }
}
